991E - Bus Number - CodeForces Solution


brute force combinatorics math *1800

Please click on ads to support us..

Python Code:

fact_dict = [1, 1]
for i in range(2, 21):
    fact_dict.append(fact_dict[-1]*i)

def find_tuples(d):
    start = [[]]
    for i in range(10):
        next_s = []
        if d[i]==0:
            for x in start:
                x2 = [y for y in x]
                next_s.append(x2+[0])
        else:
            for x in start:
                for j in range(1, d[i]+1):
                    x2 = [y for y in x]
                    next_s.append(x2+[j])
        start = next_s 
    return start

def counter1(d):
    S1 = fact_dict[sum(d)]
    for i in range(10):
        S1 = S1//fact_dict[d[i]]
    return S1 

def counter(d):
    S1 = counter1(d)
    if d[0] > 0:
        d2 = [x for x in d]
        d2[0]-=1
        S1-=counter1(d2)
    return S1
    
def process(n):
    d = [0 for i in range(10)]
    for x in str(n):
        d[int(x)]+=1
    answer = 0
    for d1 in find_tuples(d):
        answer+=counter(d1)
    print(answer)
    return

n = int(input())
process(n)


Comments

Submit
0 Comments
More Questions

236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD